package org.rcloud.io.service.impl;

import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.mapper.ShootLikeMapper;
import org.rcloud.io.mapper.ShootLike_sumMapper;
import org.rcloud.io.model.ShootLike;
import org.rcloud.io.model.ShootLike_sum;
import org.rcloud.io.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class LikeServiceImpl implements LikeService {

    @Autowired
    private ShootLikeMapper shootLikeMapper;

    @Autowired
    private ShootLike_sumMapper shootLikeSumMapper;



    @Override
    public ResultVo addLike(ShootLike shootLike) {
        if (shootLike.equals("") || shootLike == null) {
            return ResultVo.setError("传入数据为空");
        } else {
            int i1 = shootLikeMapper.selectCount(shootLike);
            if (i1 > 0) {
                int i = shootLikeMapper.deleteByCondition(shootLike);
                if (i > 0) {
                    Integer aId = shootLike.getaId();
                    ShootLike_sum selectByAId = shootLikeSumMapper.selectByAId(aId);
                    int sum = selectByAId.getSum() - 1;
                    selectByAId.setSum(sum);
                    shootLikeSumMapper.updateByPrimaryKeySelective(selectByAId);
                    return ResultVo.setOK("去消点赞成功");
                } else {
                    return ResultVo.setError("去消点赞失败");
                }
            } else {
                int i = shootLikeMapper.insert(shootLike);
                if (i > 0) {
                    Integer aId = shootLike.getaId();
                    ShootLike_sum selectByAId = shootLikeSumMapper.selectByAId(aId);
                    if (selectByAId != null && !selectByAId.equals("")) {
                        int sum = selectByAId.getSum() + 1;
                        selectByAId.setSum(sum);
                        shootLikeSumMapper.updateByPrimaryKeySelective(selectByAId);
                    } else {
                        ShootLike_sum shootLike_sum = new ShootLike_sum();
                        shootLike_sum.setaId(aId);
                        shootLike_sum.setSum(1);
                        shootLikeSumMapper.insert(shootLike_sum);
                    }
                    return ResultVo.setOK(null);
                } else {
                    return ResultVo.setError("点赞失败");
                }
            }

        }
    }


}
